package com.grantheath.erebus.view.pages 
{
	import com.grantheath.erebus.view.interfaces.IErebusTransitionHandeler;
	import com.grantheath.erebus.view.helpers.ErebusPageEvent;
	import com.grantheath.erebus.model.interfaces.IErebusAssetNode;
	import com.grantheath.erebus.view.interfaces.IErebusPage;
	import com.grantheath.erebus.model.interfaces.IErebusPageNode;

	import mx.modules.Module;

	import flash.display.DisplayObjectContainer;

	/**
	 * @author grantheath
	 */
	public class ErebusFlexPage extends Module implements IErebusPage 
	{
		private var _assets : Object;
		private var _pageNode : IErebusPageNode;
		private var _pageLayer : DisplayObjectContainer;
		private var _transitionHandeler : IErebusTransitionHandeler;

		public function initializePage(pageNode : IErebusPageNode) : void
		{
			var transitionHandelerClass : Class = pageNode.transitionHandelerClass;
			
			_pageNode = pageNode;
			_pageLayer = pageNode.pageLayer;
			_assets = {};
			transitionHandeler = new transitionHandelerClass(this, _pageLayer);
			for each(var assetNode : IErebusAssetNode in pageNode.pageAssets) {
				_assets[assetNode.assetId] = assetNode.asset;
			}
		}

		public function uninitializePage() : void
		{
			try {
				parent.removeChild(this);
			} catch(error : Error) {
			}
		}

		//SITE NAVIGATION FUNCTIONS
		public function getPage(treePath : String) : void
		{
			var getPageEvent : ErebusPageEvent = new ErebusPageEvent(ErebusPageEvent.GET_PAGE);
			
			getPageEvent.treePath = treePath;
			dispatchEvent(getPageEvent);
		}

		public function getPageWithPageNode(pageNode : IErebusPageNode) : void
		{
			var getPageEvent : ErebusPageEvent = new ErebusPageEvent(ErebusPageEvent.GET_PAGE);
			
			getPageEvent.treePath = pageNode.treePath;
			dispatchEvent(getPageEvent);
		}

		//GETTERS AND SETTERS
		public function get pageLayer() : DisplayObjectContainer
		{
			return _pageLayer;
		}

		public function get pageNode() : IErebusPageNode
		{
			return _pageNode;
		}

		public function get assets() : Object
		{
			return _assets;
		}
		
		public function get transitionHandeler() : IErebusTransitionHandeler
		{
			return _transitionHandeler;
		}
		
		public function set transitionHandeler(transitionHandeler : IErebusTransitionHandeler) : void
		{
			_transitionHandeler = transitionHandeler;
		}
	}
}
